/**
 * 
 */

function pro() {
	sendRequest();
	return false;
}
/*
function clear(){
	alert("hi");
	$("#domain").empty();
}*/
// 发送请求函数
var domainList=new Array();
function sendRequest() {
	remove("wait");
	remove("save");
	jQuery(".rate").hide();
	jQuery(".label").hide();
	var $tr = $("#one");
	$tr.siblings().remove();
	createXMLHttpRequest();
	var domain = jtrim(document.getElementById("domain").value);
	//把输入的字符以空格分开，练成一个字符串。
	var effDomain = domain.replace(/[\r\n]/g, " ");
	//以空格区分开，形成数组
	domainList = effDomain.split(" ");
	/*for(var j=0;j<domainList.length;j++){
	alert(domainList[j]);
	}
	alert("数组长度:"+domainList.length);*/
	var xx="1";
	sendDomain(domainList,xx);
}
//100
//206
//312
//418
//524
//第一次以后
var num=0;
function sendDomain(s_domain,x,name){
	var regex = "([a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,6}";
	//取得数组的长度
	var len=s_domain.length;
	var returns=parseInt(x);
	//每次查6个，计算出要查num遍
	if(len%6==0){
		num=len/6;
	}
	else{
		num=len/6+1;
		num=Math.floor(num);
	}
	if(returns<=num){
		//第一个元素位置计算公式
		var loc=6*parseInt(x)-6;
		//判断loc的数据类型
		//alert(typeof(loc));
		
		var d0=s_domain[loc]+" ";
		var d1=s_domain[loc+1]+" ";
		var d2=s_domain[loc+2]+" ";
		var d3=s_domain[loc+3]+" ";
		var d4=s_domain[loc+4]+" ";
		var d5=s_domain[loc+5];
		domains=d0+d1+d2+d3+d4+d5;
		var id = getID()+"&tran="+num+"&address="+name;
		var url = "mail/Domains";
		if (rTrim(s_domain[0]) == "") {	
			alert("对不起，查询域名不能为空，请输入查询域名。");
		} else if (s_domain.length == 1) {
			if ($.trim(s_domain[0]).search(regex) == -1) {
				alert("请输入有效域名。");
			} else {
				XMLHttpReq.open("POST", url, true);
				XMLHttpReq.setRequestHeader("Content-Type",
						"application/x-www-form-urlencoded;charset=UTF-8");
				XMLHttpReq.onreadystatechange = processResponse;
				XMLHttpReq.send("domain=" + $.trim(s_domain[0]) + "&id=" + id+"&tj="+x);
				wait();
			}
		} else {
			XMLHttpReq.open("POST", url, true);
			XMLHttpReq.setRequestHeader("Content-Type",
					"application/x-www-form-urlencoded;charset=UTF-8");
			XMLHttpReq.onreadystatechange = processResponse;
			XMLHttpReq.send("domain=" + domains + "&id=" + id+"&tj="+x);
			wait();
		}
	}
	
}
function getID() {
	var date = new Date();
	var id = date.getSeconds();
	function RndNum(n) {
		var rnd = "";
		for ( var i = 0; i < n; i++)
			rnd += Math.floor(Math.random() * 10);
		return rnd;
	}
	return RndNum(id);
}

function wait() {
	var oDiv = document.getElementById("wait");
	var oImg = document.createElement("img");
	oImg.src = "images/websedit-ag-bar.gif";
	oDiv.appendChild(oImg);

}
// 删除节点
function remove(id) {
	var test = document.getElementById(id);
	var children = test.childNodes;
	for (i = 0; i < children.length; i++) {
		test.removeChild(children[i]);
	}
}

function refresh() {
	var test = document.getElementById("wait");
	var children = test.childNodes;
	for (i = 0; i < children.length; i++) {
		test.removeChild(children[i]);
	}
	window.location.reload();
}
// 处理返回信息函数

function processResponse() {
	remove("wait");
	if (XMLHttpReq.readyState == 4) { // 判断对象状态

		if (XMLHttpReq.status == 200) { // 信息已经成功返回，开始处理信息
			DisplayHot();
			// setTimeout("DisplayHot()",1000);
		} else if (XMLHttpReq.status == 0) { // 页面不正常
			XMLHttpReq.abort();
		}
	}

}

//显示更新数据信息的函数
function DisplayHot() {
	var domain = XMLHttpReq.responseXML.getElementsByTagName("dns");
	var tjs=XMLHttpReq.responseXML.getElementsByTagName("tj")[0].firstChild.nodeValue;
	var k = 0;

	var _tbody = document.getElementById("_tbody");
	var _row;
	var _cell;
	var list=0;
	for (k = 0; k < domain.length; k++) {
		_row = document.createElement("tr");
		document.getElementById("_tbody").appendChild(_row);
		for ( var j = 0; j < 5; j++) {
			_cell = document.createElement("td");
			_row.appendChild(_cell);
		}

		if (navigator.appName.indexOf("Explorer") > -1) { // td如果是在ie下面
			_row.getElementsByTagName("td")[0].innerText=6*parseInt(tjs)-11+list;
			_row.getElementsByTagName("td")[1].innerText = XMLHttpReq.responseXML
					.getElementsByTagName("dns")[k].firstChild.nodeValue;
			_row.getElementsByTagName("td")[2].innerText = XMLHttpReq.responseXML
					.getElementsByTagName("mx")[k].firstChild.nodeValue;
			_row.getElementsByTagName("td")[3].innerText = XMLHttpReq.responseXML
					.getElementsByTagName("shortname")[k].firstChild.nodeValue;
			_row.getElementsByTagName("td")[4].innerText = XMLHttpReq.responseXML
					.getElementsByTagName("info")[k].firstChild.nodeValue;
			
		} else { // td如果是在firefox下面
			_row.getElementsByTagName("td")[0].textContent=6*parseInt(tjs)-11+list;
			_row.getElementsByTagName("td")[1].textContent = XMLHttpReq.responseXML
					.getElementsByTagName("dns")[k].firstChild.nodeValue;
			_row.getElementsByTagName("td")[2].textContent = XMLHttpReq.responseXML
					.getElementsByTagName("mx")[k].firstChild.nodeValue;
			_row.getElementsByTagName("td")[3].textContent = XMLHttpReq.responseXML
					.getElementsByTagName("shortname")[k].firstChild.nodeValue;
			_row.getElementsByTagName("td")[4].textContent = XMLHttpReq.responseXML
					.getElementsByTagName("info")[k].firstChild.nodeValue;
		

		}
		list++;
	}
	//alert(tjs+num);
	var part=((parseInt(tjs)-1)/num)*100;
	jQuery(".rate").show();
	jQuery(".mess").show().text(Math.floor(part)+"%");
	jQuery(".part").css({"width":part+"%","background":"green"});
	if(part==100){
		jQuery(".label").show().text("完成查询");
		jQuery(".mess").hide();
	}
	//取得数组的长度
	var New_len=domainList.length;
	//alert("the length is:"+domainList.length);
	var New_num=0;
	//每次查6个，计算出要查num遍
	if(New_len%6==0){
		New_num=New_len/6;
	}
	else{
		New_num=New_len/6+1;
		New_num=Math.floor(New_num);
	}
	var address = XMLHttpReq.responseXML.getElementsByTagName("address")[0].firstChild.nodeValue;
	//alert(New_num);
	if(New_num==parseInt(tjs)-1){
		if(address!="0"){
			var oDiv = document.getElementById("save");
			var oA = document.createElement("a");
			var oTxt = document.createTextNode("导出为Excel表(csv格式)");
			oA.appendChild(oTxt);
			oA.href = address;
			oA.target = "_blank";
			oDiv.appendChild(oA);
			}
	}
	//递归调用
	sendDomain(domainList,tjs,address);
	$("#_tbody tr:even td").css({"background":"#f1f1f1"});
}

// none empty
function rTrim(str) {
	var iLength;
	iLength = str.length;
	if (str.charAt(iLength - 1) == " ") {
		// 如果字串右边第一个字符为空格
		str = str.slice(0, iLength - 1);// 将空格从字串中去掉
		// 这一句也可改成 str = str.substring(0, iLength - 1);
		str = rTrim(str); // 递归调用
	}
	return str;
}
// 去左右空格
function jtrim(inputString) {
	var astr = "";
	var dstr = "";
	var flag = 0;
	for (i = 0; i < inputString.length; i++) {
		if ((inputString.charAt(i) != ' ') || (flag != 0)) {
			dstr += inputString.charAt(i);
			flag = 1;
		}
	}
	flag = 0;
	for (i = dstr.length - 1; i >= 0; i--) {
		if ((dstr.charAt(i) != ' ') || (flag != 0)) {
			astr += dstr.charAt(i);
			flag = 1;
		}
	}
	dstr = "";
	for (i = astr.length - 1; i >= 0; i--)
		dstr += astr.charAt(i);
	return dstr;
}
